using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Web.Routing;
using ExtendedMvc.Ajax;
using ExtendedMvc.Elements;

namespace ExtendedMvc {
	/// <summary>
	/// Extensions to ModelViewHelper
	/// </summary>
	public static class AjaxLinkExtensions {

		public static Link Link<T>(this ModelViewAjaxHelper<T> ajax, string content, string link, AjaxOptions ajaxOptions = null) where T : class {
			link = UrlHelper.GenerateContentUrl(link, ajax.ViewContext.HttpContext);
			return new Link(content, link).OnClick(JavaScriptHelper.GenerateAjaxScript(ajaxOptions == null ? new AjaxOptions() : ajaxOptions, "Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), {0});"));
		}

		public static Link ActionLink<T>(this ModelViewAjaxHelper<T> ajax, string content, string actionName = null, string controllerName = null, object routeValues = null, AjaxOptions ajaxOptions = null) where T : class {
			var values = routeValues is RouteValueDictionary ? routeValues as RouteValueDictionary : new RouteValueDictionary(routeValues);
			var link = UrlHelper.GenerateUrl(null, actionName, controllerName, values, ajax.RouteCollection, ajax.ViewContext.RequestContext, true);
			return ajax.Link(content, link, ajaxOptions);
		}

		public static Link RouteLink<T>(this ModelViewAjaxHelper<T> ajax, string content, string routeName = null, string protocol = null, string hostName = null, string fragment = null, object routeValues = null, AjaxOptions ajaxOptions = null) where T : class {
			var values = routeValues is RouteValueDictionary ? routeValues as RouteValueDictionary : new RouteValueDictionary(routeValues);
			var link = UrlHelper.GenerateUrl(routeName, null, null, protocol, hostName, fragment, values, ajax.RouteCollection, ajax.ViewContext.RequestContext, true);
			return ajax.Link(content, link, ajaxOptions);
		}

		public static ImageLink ImageLink<T>(this ModelViewAjaxHelper<T> ajax, string source, string link, AjaxOptions ajaxOptions = null) where T : class {
			source = UrlHelper.GenerateContentUrl(source, ajax.ViewContext.HttpContext);
			link = UrlHelper.GenerateContentUrl(link, ajax.ViewContext.HttpContext);
			return new ImageLink(source, link).OnClick(JavaScriptHelper.GenerateAjaxScript(ajaxOptions == null ? new AjaxOptions() : ajaxOptions, "Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), {0});"));
		}

		public static ImageLink ActionImageLink<T>(this ModelViewAjaxHelper<T> ajax, string source, string actionName = null, string controllerName = null, object routeValues = null, AjaxOptions ajaxOptions = null) where T : class {
			var values = routeValues is RouteValueDictionary ? routeValues as RouteValueDictionary : new RouteValueDictionary(routeValues);
			var link = UrlHelper.GenerateUrl(null, actionName, controllerName, values, ajax.RouteCollection, ajax.ViewContext.RequestContext, true);
			return ajax.ImageLink(source, link, ajaxOptions);
		}

		public static ImageLink RouteImageLink<T>(this ModelViewAjaxHelper<T> ajax, string source, string routeName = null, string protocol = null, string hostName = null, string fragment = null, object routeValues = null, AjaxOptions ajaxOptions = null) where T : class {
			var values = routeValues is RouteValueDictionary ? routeValues as RouteValueDictionary : new RouteValueDictionary(routeValues);
			var link = UrlHelper.GenerateUrl(routeName, null, null, protocol, hostName, fragment, values, ajax.RouteCollection, ajax.ViewContext.RequestContext, true);
			return ajax.ImageLink(source, link, ajaxOptions);
		}
	}
}
